<?php
namespace App\Model\Api;

use Illuminate\Support\Facades\DB;
use App\Model\BaseModel;

class RsRelationshipTasks extends BaseModel {

    protected $connection = 'api';
    protected $table = "rs_relationship_tasks";
    protected $tableLevel = "rs_relationship_level";

    protected $fields = ['id', 'content', 'rule', 'upper_limit as upperLimit', 'intimacy',
        'button_content as buttonContent', 'button_url as buttonUrl',
        'img_url as imgUrl', 'type', 'code'];
    protected $fieldsLevel = ['id','level','level_name','content','intimate','intimate_max','type'];
    protected $tablePrefix = '';
    public function __construct(array $attributes = [])
    {
        $this->tablePrefix = env('DB_PREFIX_API');
    }

    // 获取某种用户关系的增长任务
    public function getTasks(int $type, $code = ''){
        $table = $this->tablePrefix . $this->table;
        $fields = implode(',', $this->fields);
        $sql = "SELECT {$fields} FROM {$table} WHERE type=? AND is_deleted = 0";
        $rows = DB::connection('api')->select($sql, array($type));
        return self::objToArray($rows);
    }

    // 获取某种用户关系的等级
    public function getLevelList(int $type=0)
    {
        $table = $this->tablePrefix . $this->tableLevel;
        $fields = implode(',', $this->fieldsLevel);
        $sql = "SELECT {$fields} FROM {$table} WHERE is_deleted = 0";
        if (in_array($type, [1,2,3])){
            $sql .=' AND type=?';
        }
        $sql .=' ORDER BY type ASC, level ASC';
        $rows = DB::connection('api')->select($sql, array($type));
        return self::objToArray($rows);
    }

}

